//
//  PromotionViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/8.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import RxSwift
import MJRefresh

class PromotionViewController: QMUICommonViewController {


    @IBOutlet weak var tableView: UITableView!

    var questions:[ArticleItem] = [ArticleItem]()
    var currentIndex:Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        setupHeaderView()
        setupTableView()
        loadData()
        
        // Do any additional setup after loading the view.
    }
    
 
   
    
    func loadData() {
        self.showEmptyViewWithLoading()
        self.emptyView.backgroundColor = UIColor.white
        self.tableView.mj_header.beginRefreshing()
    }
    
    
    
    func setupHeaderView() {
        self.title = "TabPromotion".localized()
//        let topLogoView =  TopLogoView.loadView()
//        topLogoView.titleLabel.text = LanguageHelper.getString(key: "CFBundleDisplayName")
//
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: topLogoView)
//        let telBtn = UIBarButtonItem.qmui_item(with: #imageLiteral(resourceName: "icon_tel"), target: self, action: #selector(telClick))
//        let convertBtn = UIBarButtonItem.qmui_item(with: #imageLiteral(resourceName: "icon_covert"), target: self, action: #selector(convertClick))
//
//        self.navigationItem.rightBarButtonItems = [telBtn,convertBtn]
        
    }

    @objc func convertClick(){
        let tabVc =  self.navigationController?.tabBarController as? AppTabBarViewController
        tabVc?.showConvert()
    }

    @objc func telClick(){
        let tabVc =  self.navigationController?.tabBarController as? AppTabBarViewController
        tabVc?.telClick()
    }

    func setupTableView() {
        self.tableView.separatorStyle = .none
        self.tableView.rowHeight = 120
        self.tableView.register(UINib(nibName: "QuestionTableViewCell", bundle: nil), forCellReuseIdentifier: "cell-item")
       
        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.loadQuestions()
        })
    }
    
     func loadQuestions() {
        provider.rx.request(WE.question()).filterSuccessfulStatusCodes().mapString().subscribe(onSuccess: { (result) in
            
            if let questions = [ArticleItem].deserialize(from: result) as? [ArticleItem] {
                self.questions.removeAll()
               self.questions.append(contentsOf: questions)
                self.tableView.reloadData()
                self.tableView.mj_header.endRefreshing()
                self.hideEmptyView()
            }
        }) { (_) in
            self.showEmptyView(withText: "network error!", detailText: "", buttonTitle: nil, buttonAction: nil)
        }
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension PromotionViewController : QMUITableViewDelegate,QMUITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return questions.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell-item") as! QuestionTableViewCell
        let item = questions[indexPath.row]
        cell.item = item
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = questions[indexPath.row]
        let vc = WebViewController()
        vc.articleItem = item
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
}
